package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.EntityXfTimeZone;
import com.ocom.common.entity.payconfig.EntityXfZoneDate;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfTimeZoneService;
import com.ocom.payconfig.service.XfZoneDateService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/4/17
 **/
@Slf4j
@RestController
public class TimeZoneController {


	@Autowired
	private XfTimeZoneService xfTimeZoneService;

	@Autowired
	private XfZoneDateService xfZoneDateService;



	@Resource
	private RedisUtil redisUtil;

	/**
	 * 16.1.1 获取时区列表（getConsZoneList）
	 */
	@PostMapping(PayConfigUrl.Url_getConsZoneList)
	public Result getConsZoneList(Long comId) {
		return ResultUtil.success(xfTimeZoneService.getConsZoneList(comId));
	}

	/**
	 * 16.1.2 获取时区设置列表（getConsZoneDateList）
	 */
	@PostMapping(PayConfigUrl.Url_getConsZoneDateList)
	public Result getConsZoneDateList(Long comId,
									  @RequestParam(required = false, defaultValue = "0") Integer page,
									  @RequestParam(required = false, defaultValue = "500") Integer size) {
		return ResultUtil.success(xfZoneDateService.getConsZoneDateList(comId, page, size));
	}

	/**
	 * 16.1.3 设置日期时区（setConsDateZone）
	 */
	@PostMapping(PayConfigUrl.Url_setConsDateZone)
	public Result setConsDateZone(@RequestBody EntityXfZoneDate entityXfZoneDate) {
		EntityXfZoneDate entityXfZoneDate1 = xfZoneDateService.setConsDateZone(entityXfZoneDate.getComId(), entityXfZoneDate);
		redisUtil.dels("ConsTimeZone#comId:" + entityXfZoneDate.getComId());
		if (entityXfZoneDate1 == null) return ResultUtil.error(ResultEnum.Err_3014);
		return ResultUtil.success(entityXfZoneDate1);
	}

	/**
	 * 16.1.4 删除日期时区（removeConsDateZone）
	 */
	@PostMapping(PayConfigUrl.Url_removeConsDateZone)
	public Result removeConsDateZone(Long comId, String date) {
		Boolean b = xfZoneDateService.removeConsDateZone(comId, date);
		redisUtil.dels("ConsTimeZone#comId:" + comId);
		if (!b) return ResultUtil.error(ResultEnum.Err_3016);
		return ResultUtil.success();
	}

	/**
	 * 16.1.5 添加消费时区（addConsZone）
	 */
	@PostMapping(PayConfigUrl.Url_addConsZone)
	public Result addConsZone(@RequestBody EntityXfTimeZone entityXfTimeZone) {
		EntityXfTimeZone entityXfTimeZone1 = xfTimeZoneService.findByComIdAndNoAndName(entityXfTimeZone.getComId(),entityXfTimeZone.getZoneNo(), null);
		if (null != entityXfTimeZone1) {
			return ResultUtil.error(ResultEnum.Err_4130);
		}
		entityXfTimeZone1 = xfTimeZoneService.findByComIdAndNoAndName(entityXfTimeZone.getComId(),null, entityXfTimeZone.getZoneName());
		if (null != entityXfTimeZone1) {
			return ResultUtil.error(ResultEnum.Err_4131);
		}

		entityXfTimeZone.setStatus(1);
		xfTimeZoneService.addXfTimeZone(entityXfTimeZone.getComId(), entityXfTimeZone);
		return ResultUtil.success();
	}

	@PostMapping(PayConfigUrl.Url_updateConsZoneName)
	public Result updateConsZoneName(@RequestBody EntityXfTimeZone entityXfTimeZone){
		EntityXfTimeZone entityXfTimeZone1 = xfTimeZoneService.findConsZoneName(entityXfTimeZone.getComId(), entityXfTimeZone);
		if (null != entityXfTimeZone1) {
			return ResultUtil.error(ResultEnum.Err_4131);
		}
		xfTimeZoneService.updateConsZone(entityXfTimeZone.getComId(), entityXfTimeZone);
		return ResultUtil.success();
	}

}
